﻿using Microsoft.Owin;
using Owin;
using Hangfire;
using System.Collections.Generic;

[assembly: OwinStartupAttribute(typeof(WebApplication1.Startup))]
namespace WebApplication1
{
    public partial class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            ConfigureAuth(app);
            GlobalConfiguration.Configuration.UseSqlServerStorage("HangfireDB");
            app.UseHangfireDashboard("/hangfire",
                new DashboardOptions
                {
                    AuthorizationFilters = new[] 
                    {
                        new MyRestrictiveAuthorizationFilter() 
                    }
                });
        }
    }

    public class MyRestrictiveAuthorizationFilter : Hangfire.Dashboard.IAuthorizationFilter
    {
        public bool Authorize(IDictionary<string, object> owinEnvironment)
        {
            // In case you need an OWIN context, use the next line,
            // `OwinContext` class is the part of the `Microsoft.Owin` package.
            var context = new OwinContext(owinEnvironment);

            // Allow all authenticated users to see the Dashboard (potentially dangerous).
            return context.Authentication.User.Identity.IsAuthenticated;
        }
    }
}
